*Set working directory to Project folder

*Preamble
clear all
set more off
ssc install estout, replace

*Save data in Stata readable format
foreach csv in CONGRESS CPIAUCSL CPILFESL ELECTIONS FEDCHAIR FEDFUNDS GDPC1 GDPPOT HLWRStar PCE {
	import delimited "Data/Raw/`csv'.csv"
	save "Data/Cleaned/`csv'.dta", replace
	clear
}

*Merging the datasets
use "Data/Cleaned/CONGRESS.dta"
foreach dta in CPIAUCSL CPILFESL ELECTIONS FEDCHAIR FEDFUNDS GDPC1 GDPPOT HLWRStar PCE {
	merge 1:1 date using "Data/Cleaned/`dta'.dta"
	drop _merge
}

*Cleaning the data
gen YearString = substr(date, 1, 4)
gen MonthString = substr(date, 6, 2)
destring YearString MonthString, generate(Year Month) force
drop date YearString MonthString

gen Quarter = .
foreach quarter of numlist 1/4 {
	local prevquarter = `quarter'-1
	replace Quarter = `quarter' if Month > 3*`prevquarter' & Month <= 3*`quarter'
}

collapse demcontrolhouse-pce, by (Year Quarter)

*Setting data as a time series
gen t = yq(Year, Quarter)
format t %tq
tsset t

*Construct the inflation and output gap measures
gen Inflation = ((cpiaucsl - l4.cpiaucsl)/l4.cpiaucsl)*100
gen CoreCPIInflation = ((cpilfesl - l4.cpilfesl)/l4.cpilfesl)*100
gen PCEInflation = ((pce - l4.pce)/l4.pce)*100
gen OutputGap = ((gdpc1 - gdppot)/gdppot)*100

*Limit observations to the scope of the study
drop if Year < 1960 | Year == 2020 & Quarter >= 2 | Year >= 2021

*Estimate canonical Taylor rule and store coefficients
preserve

quietly regress fedfunds Inflation OutputGap

foreach coef of numlist 1/3 {
	gen b`coef' = r(table)[1,`coef']
}

*Construct Taylor rule implied federal funds rate data
gen rTaylor = b1*Inflation + b2*OutputGap + b3

*Set data in format for bi-annual plot (Figure 1 in Thesis)
gen EvenYears = 2*floor(Year/2)

foreach serial of numlist 1/4 {
	local serialplusfour = `serial' + 4
	replace Quarter = `serialplusfour' if Year != EvenYears & Quarter == `serial'
}

collapse fedfunds rTaylor, by(Quarter)
tsset Quarter

tsline fedfunds rTaylor, xtitle("Quarter") ytitle("Percent") note("Source: Board of Governors of the Federal Reserve, Bureau of Economic Analysis," "Bureau of Labor Statistics, and Congressional Budget Office.") legend(label (1 "Nominal Interest Rate") label(2 "Taylor Rule Implied Interest Rate")) graphregion(fcolor(white))

graph export "Data/RegOutputs/FedFundsGraph.png", as(png) name("Graph") replace

restore

*Facts about % period control
preserve
drop if Year == 1960
sum demcontrolhouse demcontrolsenate house_fedchair senate_fedchair
sum house_fedchair senate_fedchair if congelect == 1
restore

*Motivating investigation of an electoral cycle PMC
eststo: quietly newey fedfunds OutputGap Inflation rstar congelect, lag(3)
eststo: quietly newey fedfunds OutputGap Inflation rstar congelect preselect, lag(3)

eststo: quietly ivregress 2sls fedfunds rstar congelect (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar congelect preselect (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/resultstab1.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear

*Investigation of a partisan PMC
gen elections_demcontrolhouse = congelect*demcontrolhouse
gen elections_demcontrolsenate = congelect*demcontrolsenate

eststo: quietly newey fedfunds OutputGap Inflation rstar congelect demcontrolhouse elections_demcontrolhouse, lag(3)
eststo: quietly newey fedfunds OutputGap Inflation rstar congelect demcontrolsenate elections_demcontrolsenate, lag(3)

eststo: quietly ivregress 2sls fedfunds rstar congelect demcontrolhouse elections_demcontrolhouse (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar congelect demcontrolsenate elections_demcontrolsenate (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/resultstab2.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear

*Investigation of a PMC during shared Political Allegiance Periods around Elections
gen congress_fedchair = house_fedchair*senate_fedchair
gen housechairelection = congelect*house_fedchair
gen senatechairelection = congelect*senate_fedchair
gen congresschairelection = congelect*congress_fedchair

eststo: quietly newey fedfunds OutputGap Inflation rstar congelect house_fedchair housechairelection, lag(3)
eststo: quietly newey fedfunds OutputGap Inflation rstar congelect senate_fedchair senatechairelection, lag(3)
eststo: quietly newey fedfunds OutputGap Inflation rstar congelect congress_fedchair congresschairelection, lag(3)

eststo: quietly ivregress 2sls fedfunds rstar congelect house_fedchair housechairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar congelect senate_fedchair senatechairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar congelect congress_fedchair congresschairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/resultstab3.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear

///////////////////////////////////////////////////////////////////
////////////////// Excluding Nixon-Burns era///////////////////////
///////////////////////////////////////////////////////////////////

preserve

*Exclude observations from the appointment of Burns to the quarter after 1972:4 elections
drop if Year >= 1970 & Year <= 1972

*Investigation of a partisan PMC excluding N-B era
eststo: quietly reg fedfunds OutputGap Inflation rstar congelect demcontrolhouse elections_demcontrolhouse
eststo: quietly reg fedfunds OutputGap Inflation rstar congelect demcontrolsenate elections_demcontrolsenate

eststo: quietly ivregress 2sls fedfunds rstar congelect demcontrolhouse elections_demcontrolhouse (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar congelect demcontrolsenate elections_demcontrolsenate (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/nixonburns2.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear

*Investigation of a PMC during shared Political Allegiance Periods around Elections excluding N-B era
eststo: quietly reg fedfunds OutputGap Inflation rstar congelect house_fedchair housechairelection
eststo: quietly reg fedfunds OutputGap Inflation rstar congelect senate_fedchair senatechairelection
eststo: quietly reg fedfunds OutputGap Inflation rstar congelect congress_fedchair congresschairelection

eststo: quietly ivregress 2sls fedfunds rstar congelect house_fedchair housechairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar congelect senate_fedchair senatechairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar congelect congress_fedchair congresschairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/nixonburns3.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear

restore

///////////////////////////////////////////////////////////////////
// Robustness Check 1: Using Alternative Measures of Inflation/////
///////////////////////////////////////////////////////////////////

*Investigation of a partisan PMC
foreach inf in CoreCPI PCE{
	eststo: quietly newey fedfunds OutputGap `inf'Inflation rstar congelect demcontrolhouse elections_demcontrolhouse, lag(3)
	eststo: quietly newey fedfunds OutputGap `inf'Inflation rstar congelect demcontrolsenate elections_demcontrolsenate, lag(3)
	eststo: quietly ivregress 2sls fedfunds rstar congelect demcontrolhouse elections_demcontrolhouse (f1.OutputGap f1.`inf'Inflation = OutputGap `inf'Inflation l1.`inf'Inflation l2.`inf'Inflation)
	eststo: quietly ivregress 2sls fedfunds rstar congelect demcontrolsenate elections_demcontrolsenate (f1.OutputGap f1.`inf'Inflation = OutputGap `inf'Inflation l1.`inf'Inflation l2.`inf'Inflation)
	esttab using "Data/RegOutputs/`inf'2.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
	eststo clear
}

*Investigation of a PMC during shared Political Allegiance Periods around Elections
foreach inf in CoreCPI PCE{
	eststo: quietly newey fedfunds OutputGap `inf'Inflation rstar congelect house_fedchair housechairelection, lag(3)
	eststo: quietly newey fedfunds OutputGap `inf'Inflation rstar congelect senate_fedchair senatechairelection, lag(3)
	eststo: quietly newey fedfunds OutputGap `inf'Inflation rstar congelect congress_fedchair congresschairelection, lag(3)
	eststo: quietly ivregress 2sls fedfunds rstar congelect house_fedchair housechairelection (f1.OutputGap f1.`inf'Inflation = OutputGap `inf'Inflation l1.`inf'Inflation l2.`inf'Inflation)
	eststo: quietly ivregress 2sls fedfunds rstar congelect senate_fedchair senatechairelection (f1.OutputGap f1.`inf'Inflation = OutputGap `inf'Inflation l1.`inf'Inflation l2.`inf'Inflation)
	eststo: quietly ivregress 2sls fedfunds rstar congelect congress_fedchair congresschairelection (f1.OutputGap f1.`inf'Inflation = OutputGap `inf'Inflation l1.`inf'Inflation l2.`inf'Inflation)
	esttab using "Data/RegOutputs/`inf'3.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
	eststo clear
}

///////////////////////////////////////////////////////////////////
// Robustness Check 2: Limiting the Election Cycle Indicator///////
///////////////////////////////////////////////////////////////////

preserve

*Define a limelect variable that is =1 in Q2 & 3 of election years
foreach elec in congelect preselect {
	gen lim`elec' = f1.`elec'
	replace lim`elec' = 0 if lim`elec' == .
}
/*The final line in the for loop should be used with caution. It is set up to work for the specific
data used in my study as of April 13, 2023, but may need some tweaking if data is expanded*/

*Investigation of a partisan PMC with LimElect
replace elections_demcontrolhouse = limcongelect*demcontrolhouse
replace elections_demcontrolsenate = limcongelect*demcontrolsenate

eststo: quietly newey fedfunds OutputGap Inflation rstar limcongelect demcontrolhouse elections_demcontrolhouse, lag(3)
eststo: quietly newey fedfunds OutputGap Inflation rstar limcongelect demcontrolsenate elections_demcontrolsenate, lag(3)

eststo: quietly ivregress 2sls fedfunds rstar limcongelect demcontrolhouse elections_demcontrolhouse (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar limcongelect demcontrolsenate elections_demcontrolsenate (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/limelect2.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear

*Investigation of a PMC during shared Political Allegiance Periods around Elections with LimElect
replace congress_fedchair = house_fedchair*senate_fedchair
replace housechairelection = limcongelect*house_fedchair
replace senatechairelection = limcongelect*senate_fedchair
replace congresschairelection = limcongelect*congress_fedchair

eststo: quietly newey fedfunds OutputGap Inflation rstar limcongelect house_fedchair housechairelection, lag(3)
eststo: quietly newey fedfunds OutputGap Inflation rstar limcongelect senate_fedchair senatechairelection, lag(3)
eststo: quietly newey fedfunds OutputGap Inflation rstar limcongelect congress_fedchair congresschairelection, lag(3)

eststo: quietly ivregress 2sls fedfunds rstar limcongelect house_fedchair housechairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar limcongelect senate_fedchair senatechairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar limcongelect congress_fedchair congresschairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/limelect3.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear

restore

///////////////////////////////////////////////////////////////////
////// Robustness Check 3: Using Lagged Fed Funds Instead//////////
///////////////////////////////////////////////////////////////////

*Investigation of a partisan PMC with r* values
eststo: quietly newey fedfunds OutputGap Inflation l1.fedfunds congelect demcontrolhouse elections_demcontrolhouse, lag(3)
eststo: quietly newey fedfunds OutputGap Inflation l1.fedfunds congelect demcontrolsenate elections_demcontrolsenate, lag(3)

eststo: quietly ivregress 2sls fedfunds l1.fedfunds congelect demcontrolhouse elections_demcontrolhouse (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds l1.fedfunds congelect demcontrolsenate elections_demcontrolsenate (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/lagfunds2.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear

*Investigation of a PMC during shared Political Allegiance Periods around Elections with r* values
eststo: quietly newey fedfunds OutputGap Inflation l1.fedfunds congelect house_fedchair housechairelection, lag(3)
eststo: quietly newey fedfunds OutputGap Inflation l1.fedfunds congelect senate_fedchair senatechairelection, lag(3)
eststo: quietly newey fedfunds OutputGap Inflation l1.fedfunds congelect congress_fedchair congresschairelection, lag(3)

eststo: quietly ivregress 2sls fedfunds l1.fedfunds congelect house_fedchair housechairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds l1.fedfunds congelect senate_fedchair senatechairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds l1.fedfunds congelect congress_fedchair congresschairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/lagfunds3.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear

*Repeating the above after excluding Nixon-Burns era
preserve
drop if Year >= 1970 & Year <= 1972

eststo: quietly reg fedfunds OutputGap Inflation l1.fedfunds congelect house_fedchair housechairelection
eststo: quietly reg fedfunds OutputGap Inflation l1.fedfunds congelect senate_fedchair senatechairelection
eststo: quietly reg fedfunds OutputGap Inflation l1.fedfunds congelect congress_fedchair congresschairelection

eststo: quietly ivregress 2sls fedfunds l1.fedfunds congelect house_fedchair housechairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds l1.fedfunds congelect senate_fedchair senatechairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds l1.fedfunds congelect congress_fedchair congresschairelection (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/lagfunds4.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear
restore

//////////////////////////////////////////////////////////////////////////////////////////////////
//Robustness Check 4: Using Alternative Measures of Inflation and Excluding Nixon-Burns data//////
//////////////////////////////////////////////////////////////////////////////////////////////////

preserve

*Exclude observations from the appointment of Burns to the quarter after 1972:4 elections
drop if Year >= 1970 & Year <= 1972

*Investigation of a PMC during shared Political Allegiance Periods around Elections
foreach inf in CoreCPI PCE{
	eststo: quietly reg fedfunds OutputGap `inf'Inflation rstar congelect house_fedchair housechairelection
	eststo: quietly reg fedfunds OutputGap `inf'Inflation rstar congelect senate_fedchair senatechairelection
	eststo: quietly reg fedfunds OutputGap `inf'Inflation rstar congelect congress_fedchair congresschairelection
	eststo: quietly ivregress 2sls fedfunds rstar congelect house_fedchair housechairelection (f1.OutputGap f1.`inf'Inflation = OutputGap `inf'Inflation l1.`inf'Inflation l2.`inf'Inflation)
	eststo: quietly ivregress 2sls fedfunds rstar congelect senate_fedchair senatechairelection (f1.OutputGap f1.`inf'Inflation = OutputGap `inf'Inflation l1.`inf'Inflation l2.`inf'Inflation)
	eststo: quietly ivregress 2sls fedfunds rstar congelect congress_fedchair congresschairelection (f1.OutputGap f1.`inf'Inflation = OutputGap `inf'Inflation l1.`inf'Inflation l2.`inf'Inflation)
	esttab using "Data/RegOutputs/`inf'nb3.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
	eststo clear
}

restore

///////////////////////////////////////////////////////////////////
////// Robustness Check 5: Excluding Data from the 1970s///////////
///////////////////////////////////////////////////////////////////
preserve

*Exclude observations from the 1970s
drop if Year >= 1970 & Year <= 1979

*Investigation of a partisan PMC excluding observations from the 1970s
eststo: quietly reg fedfunds OutputGap Inflation rstar congelect demcontrolhouse elections_demcontrolhouse
eststo: quietly reg fedfunds OutputGap Inflation rstar congelect demcontrolsenate elections_demcontrolsenate

eststo: quietly ivregress 2sls fedfunds rstar congelect demcontrolhouse elections_demcontrolhouse (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)
eststo: quietly ivregress 2sls fedfunds rstar congelect demcontrolsenate elections_demcontrolsenate (f1.OutputGap f1.Inflation = OutputGap Inflation l1.Inflation l2.Inflation)

esttab using "Data/RegOutputs/no1970s.tex", replace star(* 0.10 ** 0.05 *** 0.01) se
eststo clear

restore
